<template>
  <div>
    <div class="sign">
      <!-- logo -->
      <div class="logo">
        <a href="/">
          <img src="./image/logo.png" />
        </a>
      </div>
      <!-- 卡片容器 -->
      <div class="signContainer">
        <!-- 下载 -->
        <div class="downloadContent">
          <a class="downloadBtn"> 下载简书APP</a>
          <!-- 二维码 -->
          <div class="downloadQrcode">
            <div class="downloadQrcodeicon"></div>
          </div>
        </div>
        <!-- 登录卡 -->
        <div class="main">
          <h4 class="title">
            <div class="normalTitle">
              <router-link to="/login" class="">登录</router-link>
              <b>·</b>
              <router-link to="/register" class="active">注册</router-link>
            </div>
          </h4>
          <!-- 账号密码 -->
          <div class="signinContainer">
            <form id="newSession">
              <!-- 昵称 -->
              <div class="input-prepend inputPerpend">
                <input
                  type="text"
                  placeholder="你的昵称"
                  v-model="name"
                  name="name"
                  v-validate="{ required: true, regex: /^[\u4e00-\u9fa5A-Za-z0-9]{1,20}$/ }"
                  :class="{ invalid: errors.has('name') }"
                />
                <i class="el-icon-user-solid"></i>
                <span class="error-msg">{{ errors.first("name") }}</span>
              </div>
              <!-- 手机号 -->
              <div class="input-prepend inputPerpend">
                <input
                  placeholder="手机号"
                  v-model="phone"
                  name="phone"
                  v-validate="{ required: true, regex: /^1\d{10}$/ }"
                  :class="{ invalid: errors.has('phone') }"
                />
                <i class="el-icon-mobile-phone"></i>
                <span class="error-msg">{{ errors.first("phone") }}</span>
              </div>
              <!-- 获取验证码 -->
              <div class="input-prepend inputPerpend">
                <input
                  placeholder="请输入你的验证码"
                  v-model="code"
                  name="code"
                  v-validate="{ required: true, regex: /^\d{6}$/ }"
                  :class="{ invalid: errors.has('code') }"
                />
                <i class="el-icon-mobile-phone"></i>
                <span class="error-msg">{{ errors.first("code") }}</span>
              </div>
              <!-- 密码 -->
              <div class="input-prepend inputPerpend">
                <input
                  type="password"
                  placeholder="设置密码"
                  v-model="password"
                  name="password"
                  v-validate="{ required: true, regex: /^[0-9A-Za-z]{8,20}$/ }"
                  :class="{ invalid: errors.has('password') }"
                />
                <i class="el-icon-lock"></i>
                <span class="error-msg">{{ errors.first("password") }}</span>
                <button style="width: 25%; height: 50px" @click="getCode">
                  获取验证码
                </button>
              </div>
              <!-- 登录按钮 -->
              <button class="signInbutton" @click="userRegister">
                <span>注册</span>
              </button>
              <p class="signUpMsg">
                点击"注册"即表示您同意并愿意遵守简书
                <br />
                <a href="">用户协议</a>
                "和"
                <a href="">隐私政策</a>
                "。"
              </p>
            </form>
            <!-- 更多登陆方式 -->
            <div class="moreSign">
              <h6>社交账号直接注册</h6>
              <ul>
                <li>
                  <a class="weixin">
                    <i class="iconfont icon-weixin"></i>
                  </a>
                </li>
                <li>
                  <a class="QQ">
                    <i class="iconfont icon-QQ"></i>
                  </a>
                </li>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "register",
  data() {
    return {
      // 收集表单数据-手机号
      phone: "",
      // 验证码
      code: "",
      // 密码
      password: "",
      // 昵称
      name: "",
    };
  },
  methods: {
    // 获取验证码
    async getCode() {
      try {
        // 如果获取到验证码
        const { phone } = this;
        phone && (await this.$store.dispatch("getCode", phone));
        //将组件的code属性值变为仓库中验证码[验证码直接自己填写了]
        this.code = this.$store.state.user.code;
        // let toPath = this.$route.query.redirect||'/'
        // this.$router.push(toPath)
      } catch (error) {}
    },
    //用户注册
    async userRegister() {
      const success = await this.$validator.validateAll();
      //全部表单验证成功，在向服务器发请求，进行祖册
      //只要有一个表单没有成功，不会发请求
      if (success) {
        try {
          const { phone, code, password, name } = this;
          await this.$store.dispatch("userRegister", {
            phone,
            code,
            password,
            name,
          });
          //注册成功进行路由的跳转
          this.$router.push("/login");
        } catch (error) {
          alert(error.message);
        }
      }
    },
  },
};
</script>

<style lang='less' scoped>
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}
a {
  text-decoration: none;
}

.sign {
  font-size: 14px;
  text-align: center;
  display: flex;
  /* align-content: center; */
  min-width: 900px;
  height: 100vh;
  background-color: #f1f1f1;
  position: relative;
}

.sign .logo {
  position: absolute;
  top: 56px;
  margin-left: 50px;
}

.sign .signContainer {
  width: 820px;
  height: 600px;
  margin: 200px auto 0;
  background-image: url(./image/sign_in.png);
  background-size: 40%;
  background-repeat: no-repeat;
  background-position: 45px 0;
  position: relative;
}

.signContainer .downloadContent {
  display: flex;
  align-items: center;
  position: absolute;
  left: 60px;
  top: 452px;
}

.downloadContent .downloadBtn {
  display: block;
  width: 252px;
  height: 46px;
  margin-right: 15px;
  background-color: #ea6f5a;
  border-radius: 30px;
  font-size: 18px;
  font-weight: 500;
  color: #fff;
  line-height: 46px;
  cursor: pointer;
}

/*二维码样式 */
.signContainer .downloadQrcode {
  width: 46px;
  height: 46px;
  position: relative;
  border-radius: 5px;
  background-image: url("./image/erweima.png");
  background-size: 38px;
  background-repeat: no-repeat;
  background-color: #fff;
  background-position: 4px 4px;
  cursor: pointer;
}

.downloadQrcode .downloadQrcodeicon {
  width: 21px;
  height: 21px;
  position: absolute;
  display: none;
  background-color: #fff;
  transform: rotate(45deg);
  box-shadow: 0 0 15px 0 rgb(0 0 0 / 15%);
  bottom: 74px;
  right: 20px;
  z-index: 1;
}

/* 登录栏样式 */
.sign .main {
  float: right;
  width: 400px;
  margin: 0 auto;
  padding: 50px 50px 30px;
  background-color: #fff;
  border-radius: 4px;
  box-shadow: 0 0 8px rgb(0 0 0 / 10%);
  vertical-align: middle;
  display: inline-block;
}
.sign .title {
  font-size: 18px;
  margin: 0 auto 50px;
  padding: 10px;
  font-weight: 400;
  color: #969696;
}
.title .active {
  font-weight: 700;
  color: #ea6f5a;
  border-bottom: 2px solid #ea6f5a;
}
.sign .title a {
  padding: 10px;
  color: #969696;
  cursor: pointer;
}

.signinContainer form {
  display: block;
  margin: 0 0 20px;
}
.inputPerpend {
  border-bottom: none;
  border-radius: 4px 4px 0 0;
}
.input-prepend input {
  width: 100%;
  height: 50px;
  margin-bottom: 0;
  padding: 4px 12px 4px 35px;
  border: 1px solid #c8c8c8;
  background-color: hsla(0, 0%, 71%, 0.1);
  vertical-align: middle;
}
.inputPerpend button {
  margin-top: -50px;
  margin-left: -100px;
  display: flex;
  float: right;
  line-height: 50px;
  text-align: center;
}
.remember {
  float: left;
  margin: 15px 0;
}
.remember span {
  font-size: 15px;
  color: #969696;
}

.forget {
  float: right;
  position: relative;
  margin: 15px 0;
  font-size: 14px;
}
.forget a {
  color: #999;
}
.sign .signInbutton {
  background-color: #42c02e;
  margin-top: 20px;
  width: 100%;
  padding: 9px 18px;
  border: none;
  border-radius: 25px;
  color: #fff;
  cursor: pointer;
  outline: none;
  display: block;
  clear: both;
}

.sign .signUpMsg {
  margin: 10px 0;
  padding: 0;
  text-align: center;
  font-size: 12px;
  line-height: 20px;
  color: #969696;
}
.sign .signUpMsg a {
  color: #3194d0;
}

.moreSign {
  margin-top: 50px;
}
.moreSign h6 {
  position: relative;
  margin: 0 0 10px;
  font-size: 12px;
  color: #b5b5b5;
}
.moreSign ul {
  margin-bottom: 10px;
  list-style: none;
  padding-left: 0;
}
.moreSign ul li {
  margin: 0 5px;
  display: inline-block;
  line-height: 20px;
}
.moreSign ul a {
  width: 50px;
  height: 50px;
  line-height: 50px;
  display: block;
  cursor: pointer;
}
.moreSign ul i {
  font-size: 32px;
}
.weixin .icon-weixin {
  color: #00bb29;
}
.QQ .icon-QQ {
  color: #498ad5;
}
</style>
